from model.Record import Record  # 假设模型定义在your_models_module.py文件中


class RecordRepository:
    def __init__(self, db_session):
        self.session = db_session

    def add(self, record):
        self.session.add(record)
        self.session.commit()

    def delete(self, record_id):
        record = self.session.query(Record).get(record_id)
        if record:
            self.session.delete(record)
            self.session.commit()
            return True
        return False

    def update(self, record_id, **kwargs):
        record = self.session.query(Record).get(record_id)
        if record:
            for key, value in kwargs.items():
                setattr(record, key, value)
            self.session.commit()
            return record
        return None

    def get(self, record_id):
        return self.session.query(Record).get(record_id)

    def get_all(self):
        return self.session.query(Record).order_by(Record.curtime.desc()).all()